﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Xml;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace ComicBookPlugin
{
    public class RSSPlugin
    {

        /// <summary>
        /// Default constructor. For completeness sake.
        /// </summary>
        public ComicBookPlugin()
        {
            
        }

        private Dictionary<string, string> fields = null;

        private List<Dictionary<string, string>> childfields = new List<Dictionary<string, string>>();

        public Dictionary<string, string> Fields
        {
            get
            {
                return fields;
            }
            set
            {
                fields = value;
            }

        }

        public List<Dictionary<string, string>> ChildFields
        {
            get
            {
                return childfields;
            }
            set
            {
                childfields = value;
            }

        }

        


        public Panel CreateContent()
        {

            // implements a jquery plugin for displaying the comic book images on the frontend
            // plugin - jQuery Cycle: http://jquery.malsup.com/cycle/

            Panel newPanel = new Panel();

            Literal header = new Literal();
            //parent.Text = "";

            header.Text = "<!-- include jQuery library -->";
            header.Text = "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js\"></script>";

            header.Text = "<!-- include Cycle plugin -->";
            header.Text = "<script type=\"text/javascript\" src=\"http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.2.74.js\"></script>";

            header.Text = "<!--  initialize the slideshow when the DOM is ready -->";
            header.Text = "<script type=\"text/javascript\">";
            header.Text = "$(document).ready(function() {";
            header.Text = "    $('.comicslideshow').cycle({";
            header.Text = "		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...";
            header.Text = "     timeout: 0,";
            header.Text = "     next: '#next2',";
            header.Text = "     prev: '#prev2'";
            header.Text = "	});";
            header.Text = "});";
            header.Text = "</script>";

            Literal title = new Literal();
            title.Text = "<h3>" + fields["title"] + "</h3>";

            Literal description = new Literal();
            description.Text = fields["description"] + "<br /><br />";

            newPanel.Controls.Add(header);
            newPanel.Controls.Add(title);
            newPanel.Controls.Add(description);

            Literal bodystart = new Literal();
            bodystart.Text = "<div class=\"comicslideshow\">";
            Literal bodyend = new Literal();
            bodyend.Text = "</div><div class=\"nav\"><a id=\"prev2\" href=\"#\">Prev</a> <a id=\"next2\" href=\"#\">Next</a></div>";

            newPanel.Controls.Add(bodystart);

            foreach (Dictionary<string, string> c in childfields)
            {

                Literal childtitle = new Literal();
                childtitle.Text = "<div><h5>" + c["title"] + "</h5>";

                Literal childimage = new Literal();
                childimage.Text = "<img src=\"" + c["image"] + "\" height=\"300\"><br /><br />";

                Literal childdate = new Literal();
                childdate.Text = "Posted on " + c["pubDate"] + "</div>";

                newPanel.Controls.Add(childtitle);
                newPanel.Controls.Add(childimage);
                newPanel.Controls.Add(childdate);

            }

            newPanel.Controls.Add(bodyend);


            /*
            //Response.Clear();
            //Response.ContentType = "text/xml";
            XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            objX.WriteStartDocument();
            objX.WriteStartElement("rss");
            objX.WriteAttributeString("version", "2.0");
            objX.WriteStartElement("channel");

            objX.WriteElementString("title", fields["title"]);
            objX.WriteElementString("link", fields["link"]);
            objX.WriteElementString("description", fields["description"]);
            objX.WriteElementString("copyright", fields["copyright"]);
            objX.WriteElementString("ttl", fields["ttl"]);


            foreach (Dictionary<string, string> c in childfields)
            {
                objX.WriteStartElement("item");
                objX.WriteElementString("title", c["title"]);
                objX.WriteElementString("description", c["description"]);
                objX.WriteElementString("link", c["link"]);
                objX.WriteElementString("pubDate", c["pubDate"]);
                objX.WriteElementString("author", c["author"]);
                objX.WriteElementString("category", c["category"]);
                objX.WriteEndElement();
            }

            objX.WriteEndElement();
            objX.WriteEndElement();
            objX.WriteEndDocument();
            objX.Flush();
            objX.Close();
            //Response.End();
             * 
             * */

            return newPanel;
            

        }


        #region iCMSPlugin Members

        public void Install()
        {

            throw new NotImplementedException();
        }

        public void Upgrade()
        {
            throw new NotImplementedException();
        }

        public void Uninstall()
        {
            throw new NotImplementedException();
        }

        public string Version()
        {
            return "0.1";
        }

        #endregion
    }
}
